# -*- coding:utf-8 -*-
'''

@File : gua64.py

@Author: 爸爸

@Date : 2019/4/28

@Desc :64卦解密
'''

import base64

# target = "大过随坤大壮乾师坎乾大过遁坤睽乾比随乾谦遁乾未济乾比遁乾离乾乾小过乾比噬嗑乾坎遁坤困乾蒙损乾无妄遁坤渐乾师家人乾颐乾乾涣乾蒙随乾随遁坤师乾蒙坎乾同人乾乾鼎乾需噬嗑乾大有革乾艮乾蒙同人乾大有革乾艮乾蒙小畜乾大有乾乾艮乾蒙随乾蛊随坤坤乾蒙需乾同人乾乾震乾需小畜乾蛊随乾革乾蒙需乾随革乾丰乾需小畜乾随革乾丰乾需需乾随随乾鼎乾需随乾蛊乾乾革乾需无妄乾谦随坤屯乾蒙遁乾"

table = {
    "乾": "A", "坤": "B", "屯": "C", "蒙": "D", "需": "E", "讼": "F", "师": "G", "比": "H",
    "小畜": "I", "履": "J", "泰": "K", "否": "L", "同人": "M", "大有": "N", "谦": "O", "豫": "P",
    "随": "Q", "蛊": "R", "临": "S", "观": "T", "噬嗑": "U", "贲": "V", "剥": "W", "复": "X",
    "无妄": "Y", "大畜": "Z", "颐": "a", "大过": "b", "坎": "c", "离": "d", "咸": "e", "恒": "f",
    "遁": "g", "大壮": "h", "晋": "i", "明夷": "j", "家人": "k", "睽": "l", "蹇": "m", "解": "n",
    "损": "o", "益": "p", "夬": "q", "姤": "r", "萃": "s", "升": "t", "困": "u", "井": "v",
    "革": "w", "鼎": "x", "震": "y", "艮": "z", "渐": "0", "归妹": "1", "丰": "2", "旅": "3",
    "巽": "4", "兑": "5", "涣": "6", "节": "7", "中孚": "8", "小过": "9", "既济": "+", "未济": "/"}

'''
密文转明文
'''


def tar2res(tar):
    for k, v in table.items():
        tar = tar.replace(k, v)
    res = base64.b64decode(tar).decode()
    return res


'''
明文转密文
'''


def res2tar(res):
    res = base64.b64encode(bytes(res, 'ASCII')).decode()
    for k, v in table.items():
        res = res.replace(v, k)
    return res


if __name__ == '__main__':
    # res = "magnet:?xt=urn:btih:E65281B021061EE15C2BC4E16C2AFCD0C58AE9FF&dn=%5B168x.me%5Did-036"
    res = "magnet:?xt =urn:btih:9c9032a798e0f7a265340e5e09903e9564c57f89"
    # res="magnet:?xt=urn:btih:RFWTABKGKEHZ5CUGUUXJNSMGLRML4HNY&tr=http://anidex.moe:6969/announce"
    tar = res2tar(res)
    print(tar)
    print(tar2res(tar))
